package com.cmf.runex.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.cmf.runex.constant.CommoneConstant;
import com.cmf.runex.domain.AndroidEx;
import com.cmf.runex.service.AndroidExService;

@Controller
public class AndroidExController {

	@Resource
	private AndroidExService androidExService;

	@RequestMapping("/getAndroidExList")
	public ModelAndView getAndroidExList() {
		ModelAndView mav = new ModelAndView("apprun");
		mav.addObject("androidList", androidExService.getAndroidExList());
		return mav;
	}

	@ResponseBody
	@RequestMapping("/getOneAndroidEx")
	public AndroidEx getOneAndroidEx(String clientId) {
		return androidExService.getOneAndroidEx(clientId);
	}

	@ResponseBody
	@RequestMapping("/addAndroidEx")
	public int addAndroidEx(AndroidEx androidEx) {
		return androidExService.addAndroidEx(androidEx);
	}

	@ResponseBody
	@RequestMapping("/updateAndroidEx")
	public int updateAndroidEx(AndroidEx androidEx) {
		androidExService.updateAndroidEx(androidEx);
		return 222;
	}

	@ResponseBody
	@RequestMapping("/checkSetting")
	public AndroidEx checkAndroidExSetting(AndroidEx androidEx,HttpServletRequest request) {

		return androidExService.checkAndroidEx(androidEx,request);
	}

	/*
	 * 采用file.Transto 来保存上传的文件
	 */
	@RequestMapping("/fileUpload")
	public void fileUpload(HttpServletRequest request, @RequestParam("file") CommonsMultipartFile file, String clientId)
			throws IOException {

		AndroidEx aEx = getOneAndroidEx(clientId);
		String realPath = aEx.getClientId() + "_" + aEx.getPhoneModle();
		String fileName = file.getOriginalFilename();
		String path1 = request.getSession().getServletContext().getRealPath(realPath) + File.separator;
		System.out.println(path1);
		File localFile = new File(path1 + fileName);
		if (!localFile.exists()) {
			localFile.mkdirs();
		}
		file.transferTo(localFile);

		// 更新下载地址
//		aEx.setmPagAdd(
//				"http://192.168.8.117:8080/ssm_project/download?fileName=" + fileName + "&clientId=" + aEx.getClientId());
		aEx.setmPagAdd(
				"http://pro.cconon.com:8080/ssm_project/download?fileName=" + fileName + "&clientId=" + aEx.getClientId());
		updateAndroidEx(aEx);
	}

	@RequestMapping("/download")
	public String download(String fileName, HttpServletRequest request, HttpServletResponse response, String clientId) {
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
		try {
			AndroidEx aEx = getOneAndroidEx(clientId);
			String realPath = aEx.getClientId() + "_" + aEx.getPhoneModle();
			String name = request.getSession().getServletContext().getRealPath(realPath) + File.separator;

			InputStream inputStream = new FileInputStream(new File(name + fileName));

			OutputStream os = response.getOutputStream();
			byte[] b = new byte[2048];
			int length;
			while ((length = inputStream.read(b)) > 0) {
				os.write(b, 0, length);
			}

			// 这里主要关闭。
			os.close();

			inputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 返回值要注意，要不然就出现下面这句错误！
		// java+getOutputStream() has already been called for this response
		return null;
	}

}
